package com.clouddot.baipai.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.infrastructure.environment.Environment;
import com.clouddot.baipai.ui.fragment.A_Set1Fragment;
import com.clouddot.baipai.ui.fragment.A_Set2Fragment;
import com.clouddot.baipai.ui.fragment.A_Set3Fragment;
import com.clouddot.baipai.ui.util.SelectedBtnUtil;

/**
 * Created by wxy on 2018/4/20.
 */

public class H_SettingActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    private LinearLayout[] linearLayouts = new LinearLayout[4];
    private LinearLayout back;
    private ScrollView scrollView1,scrollView2;
    private Animation animation1,animation2;
    private Fragment[] fragments = new Fragment[3];
    private int currentShowFragmentIndex = 0;
    private TextView carddevid ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Environment.getInstance().activities.add(this);

    }

    @Override
    protected void setContentView() {
        super.setContentView();
        if(Environment.getInstance().Height <= 100)
        {
            setContentView(R.layout.h_a_setting);
        }else{
            setContentView(R.layout.v_a_setting);
        }
    }
    @Override
    protected void initControls() {
        super.initControls();
        linearLayout1 =  find(R.id.setting);
        linearLayout2 =  find(R.id.update);
        linearLayout3 =  find(R.id.Password);
        linearLayout4 =  find(R.id.net);
        back =  find(R.id.btn1);
        linearLayout1.setSelected(true);

        fragments[0] = new A_Set1Fragment();
        fragments[1] = new A_Set2Fragment();
        fragments[2] = new A_Set3Fragment();

        linearLayouts[0] = linearLayout1;
        linearLayouts[1] = linearLayout2;
        linearLayouts[2] = linearLayout3;
        linearLayouts[3] = linearLayout4;

        scrollView1 = find(R.id.left);
        scrollView2 = find(R.id.right);

        animation2 = AnimationUtils.loadAnimation(H_SettingActivity.this, R.anim.buttom_start_anim);
        animation1 = AnimationUtils.loadAnimation(H_SettingActivity.this,R.anim.buttom_start_anim2);
        animation1.setDuration(500);
        animation2.setDuration(500);

        scrollView1.startAnimation(animation1);
        scrollView2.startAnimation(animation2);

        carddevid = find(R.id.Wlanno);
        carddevid.setText(Env.getAppContext().carddevid);
    }

    @Override
    protected void setListener() {
        super.setListener();
        back.setOnClickListener(this);

        for (LinearLayout linearLayout : linearLayouts)
            linearLayout.setOnClickListener(this);

        showFragment(0);

    }

    @Override
    protected void loadParams() {
        super.loadParams();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Env.getAppContext().isInitial){
            linearLayout2.setVisibility(View.VISIBLE);
            linearLayout3.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void runThreadAction(String actionName) {
        super.runThreadAction(actionName);
    }

    @Override
    protected Object runThreadActionExceute(String actionName) {
        return super.runThreadActionExceute(actionName);

    }

    @Override
    protected void runThreadActionSuccess(String actionName, Object result) {
        super.runThreadActionSuccess(actionName, result);

    }

    @Override
    protected void runThreadActionError(String actionName, Exception ex) {
        super.runThreadActionError(actionName, ex);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                finish();
                break;
            case R.id.update:
                SelectedBtnUtil.selectedforLinear(linearLayouts,linearLayouts[1]);
                showFragment(1);
                break;

            case R.id.setting:
                SelectedBtnUtil.selectedforLinear(linearLayouts,linearLayouts[0]);
                showFragment(0);
                break;
            case R.id.Password:
                SelectedBtnUtil.selectedforLinear(linearLayouts,linearLayouts[2]);
                showFragment(2);
                break;
            case R.id.net:
                Env.getAppContext().isOpenService = false;
                SelectedBtnUtil.selectedforLinear(linearLayouts,linearLayouts[3]);
                Intent intent =  new Intent(Settings.ACTION_SETTINGS);
//                Intent intent =  new Intent(Settings.ACTION_WIFI_SETTINGS);
                startActivityForResult(intent,1);
                break;
        }
    }
    /**
     *
     * 点击按钮切换fragment
     */
    private void showFragment(int index){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        // 动作1:隐藏当前
        transaction.hide(fragments[currentShowFragmentIndex]);
        Fragment showFragment = null;
        // 动作2:添加要显示的
        showFragment = fragments[index];

        if (!showFragment.isAdded())
        {
            transaction.replace(R.id.setfg, showFragment);
        }
        // 动作3:显示出来
        transaction.show(showFragment);
        // 提交事务
        transaction.commit();
        currentShowFragmentIndex = index;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SelectedBtnUtil.selectedforLinear(linearLayouts,linearLayouts[currentShowFragmentIndex]);
        showFragment(currentShowFragmentIndex);
        Env.getAppContext().isOpenService = (boolean) Env.getHelper().get("isOpenService", false);


    }
}
